package com.ryskoo.customer.customerintegral.service;

import com.ryskoo.catering.consumestatistics.domain.ConsumeStatisticsPO;
import com.ryskoo.catering.consumestatistics.service.IConsumeStatisticsService;
import com.ryskoo.common.hclconstant.IntegralTypeEnum;
import com.ryskoo.customer.customerintegral.domain.CustomerIntegralPO;
import com.ryskoo.customer.customerintegral.repository.ICustomerIntegralRepository;
import com.ryskoo.customer.customerintegrallog.service.ICustomerIntegralLogService;
import com.ryskoo.framework.core.exception.InvalidParamException;
import com.ryskoo.framework.core.service.BasicServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @author BeHappy
 */
@Service
public class CustomerIntegralServiceImpl extends BasicServiceImpl<CustomerIntegralPO> implements ICustomerIntegralService {
    @Autowired
    private ICustomerIntegralRepository customerIntegralRepository;

    @PostConstruct
    public void init() {
        this.setDao(customerIntegralRepository);
    }
    @Autowired
    private ICustomerIntegralLogService customerIntegralLogService;
    @Autowired
    private IConsumeStatisticsService consumeStatisticsService;

    /**
     * 获取会员积分
     *
     * @param customerUuid
     * @param vendorUuid
     * @return
     */
    @Override
    public CustomerIntegralPO getIntegral(String customerUuid, String vendorUuid) {
        return customerIntegralRepository.getIntegral(customerUuid, vendorUuid);
    }

    /**
     * 赠送积分
     *
     * @param customerUuid
     * @param vendorUuid
     * @param orderId
     * @param giveIntegral
     * @param type
     */
    @Override
    public void add(String customerUuid, String vendorUuid, String orderId, int giveIntegral, int type) {
        CustomerIntegralPO po = getIntegral(customerUuid, vendorUuid);
        if(null == po) {
            po = new CustomerIntegralPO();
            po.setCustomerUuid(customerUuid);
            po.setVendorUuid(vendorUuid);
            po.setIntegral(giveIntegral);
            po.setUsableIntegral(giveIntegral);
            this.create(po);
            customerIntegralLogService.createLog(po.getUuid(), orderId, giveIntegral, type);
        } else {
            po.setIntegral(po.getIntegral() + giveIntegral);
            po.setUsableIntegral(po.getUsableIntegral() + giveIntegral);
            this.update(po);
            customerIntegralLogService.createLog(po.getUuid(), orderId, giveIntegral, type);
        }
    }

    /**
     * 使用积分
     *
     * @param customerUuid
     * @param vendorUuid
     * @param orderId
     * @param useIntegral
     * @param type
     */
    @Override
    public void use(String customerUuid, String vendorUuid, String orderId, int useIntegral, int type) {
        CustomerIntegralPO po = getIntegral(customerUuid, vendorUuid);
        if(null == po){
            throw new InvalidParamException("会员积分为空");
        }
        if(po.getUsableIntegral() < useIntegral){
            throw new InvalidParamException("可用积分不足");
        }
        po.setUsableIntegral(po.getUsableIntegral() - useIntegral);
        this.update(po);
        //使用积分时，积分日志记录负数
        customerIntegralLogService.createLog(po.getUuid(), orderId, -useIntegral, type);
    }

    /**
     * 下单赠送积分
     *
     * @param customerUuid
     * @param vendorUuid
     * @param orderId
     * @param giveIntegral
     */
    @Override
    public void addByOrder(String customerUuid, String vendorUuid, String orderId, int giveIntegral) {
        add(customerUuid, vendorUuid, orderId, giveIntegral, IntegralTypeEnum.ORDER_PRESENTED.getValue());
    }

    /**
     * 取消订单退还使用的积分
     *
     * @param customerUuid
     * @param vendorUuid
     * @param orderId
     * @param useIntegral
     */
    @Override
    public void addByCancelOrder(String customerUuid, String vendorUuid, String orderId, int useIntegral) {
        add(customerUuid, vendorUuid, orderId, useIntegral, IntegralTypeEnum.CANCEL_ORDER_RETURN.getValue());
    }

    
    /**
     * 订单评价赠送积分
     *
     * @param customerUuid
     * @param vendorUuid
     * @param orderId
     * @param giveIntegral
     */
    @Override
    public void addByAppraise(String customerUuid, String vendorUuid, String orderId, int giveIntegral) {
        add(customerUuid, vendorUuid, orderId, giveIntegral, IntegralTypeEnum.APPRAISE_PRESENTED.getValue());
    }
    
    /**
     * 下单使用积分
     *
     * @param customerUuid
     * @param vendorUuid
     * @param orderId
     * @param useIntegral
     */
    @Override
    public void useByOrder(String customerUuid, String vendorUuid, String orderId, int useIntegral) {
        use(customerUuid, vendorUuid, orderId, useIntegral, IntegralTypeEnum.ORDER_USE.getValue());
    }

    /**
     * 查询会员积分、消费次数、消费金额
     *
     * @param customerUuid
     * @param vendorUuid
     * @return
     */
    @Override
    public Map<String, Object> getStatistics(String customerUuid, String vendorUuid) {
        if(StringUtils.isEmpty(customerUuid)){
            throw new InvalidParamException("会员uuid为空");
        }
        if(StringUtils.isEmpty(vendorUuid)){
            throw new InvalidParamException("商户uuid为空");
        }
        Map<String, Object> map = new HashMap<>();
        CustomerIntegralPO integralPO = this.getIntegral(customerUuid, vendorUuid);
        ConsumeStatisticsPO statisticsPO = consumeStatisticsService.getByVendorAndCustomer(vendorUuid, customerUuid);
        if(null != integralPO){
            map.put("usableIntegral", integralPO.getUsableIntegral());
        }else{
            map.put("usableIntegral", 0);
        }
        if(null != statisticsPO){
            map.put("time", statisticsPO.getTime());
            map.put("money", statisticsPO.getMoney());
        }else{
            map.put("time", 0);
            map.put("money", 0);
        }
        return map;
    }
}
